//
//  YSY_HeroVideo_ViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YSY_HeroVideo_ViewController.h"
#import "YSY_HeroVideo_Model.h"
#import "YSY_Player_ViewController.h"
#import "YSY_HeroVideo_TableViewCell.h"
#define VIDEOLISTURL(page,heroName) [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=l&p=%ld&tag=%@", page, heroName]
@interface YSY_HeroVideo_ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) NSInteger refreshPage;    //刷新页数
@property (nonatomic, assign) NSInteger refreshTotalPage;   //刷新总页数

@property (nonatomic, retain) NSMutableArray *heroVideoArr;     //英雄视频数组
@property (nonatomic, retain) MBProgressHUD *progressHUD;
@end

@implementation YSY_HeroVideo_ViewController
- (void)dealloc
{
    self.myTable.delegate = nil;
    self.myTable.dataSource = nil;
    [_progressHUD release];
    [_heroName release];
    [_myTable release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    

}

- (void)createView {
    
    self.myTable = [UITableView new];
    [self.view addSubview:self.myTable];
    [self.myTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    
//    self.myTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
//    
////    if ((self.myTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:nil])) {
////        [self.myTable.mj_footer beginRefreshing];
////    }
    
    [self.myTable registerClass:[YSY_HeroVideo_TableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
#pragma mark - 数据
- (void)getData {
    [self.myTable.mj_header beginRefreshing];
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.progressHUD];
    self.progressHUD.labelText = @"Loading";
    [self.progressHUD show:YES];
    [self.progressHUD release];
    
    [GoodManNews_AFNetTool getNetWithURL:VIDEOLISTURL(self.refreshPage, self.heroName) body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        self.heroVideoArr = [YSY_HeroVideo_Model baseModelWithArr:responseObject];
        NSNumber *number = responseObject[0][@"totalPage"];
        self.refreshTotalPage = [number integerValue];
        NSLog(@"+++++%ld", self.refreshTotalPage);
        [self.myTable reloadData];
//        [self.myTable.mj_header endRefreshing];
//        [self.myTable.mj_footer endRefreshing];
        [self.progressHUD hide:YES];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}
#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.heroVideoArr) {
        return self.heroVideoArr.count;
    }else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSY_HeroVideo_Model *model = self.heroVideoArr[indexPath.row];
    YSY_HeroVideo_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.videoImageV sd_setImageWithURL:[NSURL URLWithString:model.cover_url] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
    cell.videoTotalTimeL.text = [self changeVideoTime:model.video_length];
    cell.videoTimeL.text = model.upload_time;
    cell.videoTitleL.text = model.title;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"播放视频");
    YSY_HeroVideo_Model *model = self.heroVideoArr[indexPath.row];
    [self.delegate jumpToPlayerVC:model.vid];
}

#pragma mark - 私有方法
//计算时间
- (NSString *)changeVideoTime:(NSInteger)time {
    NSInteger minute = time / 60;
    NSInteger second = time % 60;
    NSString *timeStr = [NSString stringWithFormat:@"%02ld:%02ld", minute, second];
    return timeStr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
